// CardCollection_BPA.cpp: implementation of the CCardCollection_BPA class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "../../Include/Bse/CardCollection_BPA.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCardCollection_BPA::CCardCollection_BPA()
{
	m_nExistCount = 0;
	m_pFileRows = NULL;
}

CCardCollection_BPA::~CCardCollection_BPA()
{

}

int CCardCollection_BPA::Add(CCard_BPA *pCard)
{
	if (pCard == NULL)
		return -1;

	CList::Add(pCard);

	m_nExistCount++;
	pCard->m_nCardState = Added;

	return m_nExistCount;
}

int CCardCollection_BPA::GetCount()
{
	return m_nExistCount;
}

CCard_BPA* CCardCollection_BPA::GetCard(CCard_BPA *pCard)
{
	CCard_BPA* pCurtCard;

	if (!pCard)
		return NULL;
	
	for(pCurtCard=(CCard_BPA*)m_pHead;pCurtCard;pCurtCard=(CCard_BPA*)pCurtCard->m_pNextItem)
	{
		if (pCurtCard->m_nCardState!=Deleted && pCurtCard->Equals(pCard))
			break;
	}
	
	return pCurtCard;

/*	CCard_BPA* pRetCard = (CCard_BPA*)CList::GetItem(pCard);

	if (pRetCard && pRetCard->m_nCardState == Deleted)
		return NULL;

	return pRetCard;*/
}

CCard_BPA* CCardCollection_BPA::GetFirstCard()
{
	CCard_BPA* pCurtCard = (CCard_BPA*)m_pHead;
	
	while (pCurtCard)
	{
		if (pCurtCard->m_nCardState != Deleted)
			return pCurtCard;
		pCurtCard = (CCard_BPA*)pCurtCard->m_pNextItem;
	}
	
	return pCurtCard;
}

CCard_BPA* CCardCollection_BPA::Traverse(CCard_BPA *pCard, int nDirection)
{
	CCard_BPA* pCurtCard = (CCard_BPA*)CList::Traverse(pCard,nDirection);
	while (pCurtCard)
	{
		if (pCurtCard->m_nCardState != Deleted)
			return pCurtCard;

		pCurtCard = (CCard_BPA*)CList::Traverse(pCurtCard,nDirection);
	}

	return pCurtCard;
}

void CCardCollection_BPA::Detach()
{
	CCard_BPA* pCard = (CCard_BPA*)m_pHead;
	CCard_BPA* pDelCard = NULL;
	while(pCard)
	{
		if (pCard->m_nCardState == Deleted)
		{
			pDelCard = pCard;
			pCard = (CCard_BPA*)pCard->m_pNextItem;
			pDelCard->CListItem::RemoveFromList();
		}
		else
		{
			pCard->m_nCardState = Added;
			pCard->m_pFileRow = NULL;
			pCard = (CCard_BPA*)pCard->m_pNextItem;
		}
	}
}

bool CCardCollection_BPA::Update()
{
	if (!m_pFileRows)
	{
		printf("Error in (CCardCollection_BPA: Update): m_pFileRows is NULL!\n");
		return false;
	}

	CCard_BPA* pDelCard = NULL;
	CCard_BPA* pCurtCard = (CCard_BPA*)m_pHead;

	while (pCurtCard)
	{
		switch(pCurtCard->m_nCardState)
		{
		case Added:
			if (pCurtCard->m_pFileRow)
			{
				printf("Error in (CCardCollection_BPA: Update): card %s!\n",pCurtCard->m_strCardType);
				return false;
			}

			pCurtCard->m_pFileRow = new CFileRow;

			if (!pCurtCard->Update())
			{
				printf("Error in (CCardCollection_BPA: Update): impossible!\n");
				return false;
			}

			m_pFileRows->Add(pCurtCard->m_pFileRow);
			pCurtCard->m_nCardState = Unchanged;
			pCurtCard=(CCard_BPA*)pCurtCard->m_pNextItem;

			break;
		case Deleted:
			pDelCard = pCurtCard;
			pCurtCard=(CCard_BPA*)pCurtCard->m_pNextItem;
			if (!pDelCard->Update())
			{
				printf("Error in (CCardCollection_BPA: Update): impossible!\n");
				return false;
			}

			pDelCard->CListItem::RemoveFromList();
			break;
		case Modified:
			if (!pCurtCard->Update())
			{
				printf("Error in (CCardCollection_BPA: Update): impossible!\n");
				return false;
			}
			pCurtCard->m_nCardState = Unchanged;

			pCurtCard=(CCard_BPA*)pCurtCard->m_pNextItem;
			break;
		default:
			pCurtCard=(CCard_BPA*)pCurtCard->m_pNextItem;
			break;
		}
	}

	return true;
}

bool CCardCollection_BPA::ExchangeCards(CCard_BPA *pCard1, CCard_BPA *pCard2)
{
	if (!m_pFileRows)
		return false;

	CFileRow *pFileRow1, *pFileRow2;
	if (CList::ExchangeItems(pCard1,pCard2))
	{
		pFileRow1 = pCard1->m_pFileRow;
		pFileRow2 = pCard2->m_pFileRow;
		m_pFileRows->ExchangeFileRows(pFileRow1,pFileRow2);
		return true;
	}
	return false;
}
